/**
 * 
 */
package com.etc.filter;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;
import org.springframework.beans.factory.annotation.Autowired;

import com.etc.user.service.IUserService;
import com.etc.user.vo.UserVO;

/**
 * @author Administrator
 *
 */
public class RememberAuthenticationFilter extends FormAuthenticationFilter {
	
	@Autowired
	private IUserService userService;

	@Override
	protected boolean isAccessAllowed(ServletRequest request,
			ServletResponse response, Object mappedValue) {
		 Subject subject = getSubject(request, response);  
		 if(!subject.isAuthenticated() && subject.isRemembered()){  
			 Session session=subject.getSession(true);
			 if(session.getAttribute(subject.getPrincipal().toString())==null){
				 String username = subject.getPrincipal().toString();  
				 UserVO cmsUser=userService.findByName(username);
				 session.setAttribute(username, cmsUser);
			 }
		 }
		return subject.isAuthenticated() || subject.isRemembered();  
	}
	
}
